package io.github.wanggit.antrpc.console.zookeeper;

import io.github.wanggit.antrpc.console.web.vo.ZookeeperNodeVO;
import org.springframework.stereotype.Component;

import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Component
public class AllNodeContainer implements IAllNodeContainer {

    private final ConcurrentHashMap<String, ZookeeperNodeVO> datas = new ConcurrentHashMap<>();

    @Override
    public void add(ZookeeperNodeVO vo) {
        datas.put(vo.getPath(), vo);
    }

    @Override
    public void update(ZookeeperNodeVO vo) {
        datas.put(vo.getPath(), vo);
    }

    @Override
    public void delete(ZookeeperNodeVO vo) {
        datas.remove(vo.getPath());
    }

    @Override
    public ZookeeperNodeVO findByPath(String path) {
        return datas.get(path);
    }

    @Override
    public List<ZookeeperNodeVO> findAll(String keyword) {
        return datas.values().stream()
                .filter(
                        it ->
                                null == keyword
                                        || it.getPath()
                                                .toLowerCase()
                                                .contains(keyword.toLowerCase()))
                .sorted(Comparator.comparing(ZookeeperNodeVO::getPath))
                .collect(Collectors.toList());
    }
}
